﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading.Tasks;
using System.Threading;
using ConsoleApplication27;

namespace WpfApplication11
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }



        private void radioButton1_Checked(object sender, RoutedEventArgs e)
        {
            button3.IsEnabled = true;
            button2.IsEnabled = true;
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            button5.Visibility = Visibility.Visible;
            button6.Visibility = Visibility.Visible;

        }

        private void button6_Click(object sender, RoutedEventArgs e)
        {
            ulong system = 256; //system

            Liczba a;


            if (radioButton1.IsChecked == true) a = new Liczba("255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255", system);
            else
                if (radioButton2.IsChecked == true) a = new Liczba("255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255", system);
                else
                    a = new Liczba("255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255", system);

            a.znak = '+';

            Random r = new Random();
            RabinMiller rabin = new RabinMiller();

            rabin.primes = new List<Liczba>();

            int i = 0;
            DateTime before = DateTime.Now;

            while (rabin.primes.Count < 2) { rabin.test(Pomoce.randomodd(a, r), r); Console.WriteLine(i++); }

            RSA.makekeys(rabin.primes[0], rabin.primes[1]);


        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {

            ulong system = 256; //system

            Liczba a;


            if (radioButton1.IsChecked == true) a = new Liczba("255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255", system);
            else
                if (radioButton2.IsChecked == true) a = new Liczba("255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255", system);
                else
                    a = new Liczba("255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255", system);

            a.znak = '+';



            RabinMiller rabin = new RabinMiller();
            int i = 0;
            Random r = new Random();

            Action<ParallelLoopState> processQueue = (lps) =>
            {
                rabin.test(Pomoce.randomodd(a, r), r);
                i++;

            };

            Func<bool> dwiepierwsze = () => rabin.primes.Count < 2;


            processQueue.InParallelWhile(dwiepierwsze);

            RSA.makekeys(rabin.primes[0], rabin.primes[1]);


        }

        private void button1_Click_1(object sender, RoutedEventArgs e)
        {

            io plik = new io();
            Microsoft.Win32.OpenFileDialog open = new Microsoft.Win32.OpenFileDialog();
            bool result = (bool)open.ShowDialog();

            List<Liczba> zakodowe = RSA.szyfruj(open.FileName);

            Microsoft.Win32.SaveFileDialog save = new Microsoft.Win32.SaveFileDialog();
            result = (bool)save.ShowDialog();
            plik.zapisz(zakodowe, save.FileName);
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            io plik = new io();
            Microsoft.Win32.OpenFileDialog open = new Microsoft.Win32.OpenFileDialog();
            bool result = (bool)open.ShowDialog();

            List<Liczba> odszyfrowane = RSA.odszyfruj(open.FileName);

            Microsoft.Win32.SaveFileDialog save = new Microsoft.Win32.SaveFileDialog();
            result = (bool)save.ShowDialog();
            plik.zapisz(odszyfrowane, save.FileName);




        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            io plik = new io();
            Microsoft.Win32.OpenFileDialog open = new Microsoft.Win32.OpenFileDialog();
            bool result = (bool)open.ShowDialog();
            plik.wczytajklucze(open.FileName);


        }
    }
}
